package manouevre;

/**
 * author :  apurv verma
 * email  :  dapurv5@gmail.com
 */

public class NestedLoopsViaRecursion {
	
	public static void fnested(){
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				for(int k=0;k<3;k++){
					System.out.println(i+","+j+","+k);
				}
			}
		}
	}
	//BOTH FUNCTIONS frecursion() and fnested() DO THE SAME THING.
	public static void main(String args[]){
		//fnested();
		frecursion(0,0,0);
		genericFRecursion(new int[]{0,0,0,0},3);
	}
	
	public static void frecursion(int a,int b,int c){
		if(c>=3){
			c=0;
			b++;
			frecursion(a,b,c);
			return;
		}
		if(b>=3){
			b=0;
			a++;
			frecursion(a,b,c);
			return;
		}
		if(a>=3){
			return;
		}
		System.out.println(a+","+b+","+c);
		frecursion(a,b,c+1);
	}
	/*
	 * A.length represents number of nested loops.
	 * Initial value of A[i] represents the initial value of ith nested loop
	 * l represents the terminal value of ith nested loop.
	 */
	public static void genericFRecursion(int A[],int n){
		for(int i=0;i<A.length;i++){
			if(A[i]>=n){
				A[i]=0; //Assigned the initial value.
				if(i-1>=0){
					A[i-1]++;
					genericFRecursion(A,n);
					return;
				}
				else{
					return;
				}
			}
		}
			for(int j=0;j<A.length;j++){
			System.out.print(A[j]+",");
			}
			System.out.println();
			A[A.length-1]++;
			genericFRecursion(A,n);
			
		}
	}

